﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace VideoClipNameHelper
{
    public
    class Serialization
    {
        //
        public static readonly string DataFileName = "VideoFileDump.xml";
        //
        public static int Serialize(List<VideoFile> vfl, int maxCount=int.MaxValue)
        {
            int n = 0;

            List<string> sd = new List<string>();
            DataContractSerializer serializer = new DataContractSerializer(typeof(VideoFile));
            //
            if (System.IO.File.Exists(DataFileName))
            {
                System.IO.File.Delete(DataFileName);
            }
            //
            //using (System.IO.StreamWriter file = new System.IO.StreamWriter(DataFileName, true, Encoding.UTF8))
            {
                foreach (VideoFile vf in vfl)
                {
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        serializer.WriteObject(ms, vf);
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
                        string content = new System.IO.StreamReader(ms).ReadToEnd();
                        //file.WriteLine(content);
                        sd.Add(content);
                        n++;
                    }
                    if (n >= maxCount) break;
                }
            }
            //
            System.IO.File.WriteAllLines(DataFileName, sd.ToArray(), Encoding.UTF8);
            //
            return n;
        }


        public static int DeSerialize(ref List<VideoFile> vfl, int maxCount = int.MaxValue)
        {
            int n = 0;

            if (!System.IO.File.Exists(DataFileName))
            {
                return 0;
            }
            //
            DataContractSerializer serializer = new DataContractSerializer(typeof(VideoFile));
            //
            string [] ll = System.IO.File.ReadAllLines(DataFileName, Encoding.UTF8);
            vfl = new List<VideoFile>(ll.Count() + 100);
            //using (System.IO.StreamReader file = new System.IO.StreamReader(DataFileName, Encoding.UTF8))
            foreach(string l in ll)
            {
                //string l;
                //while(null != (l = file.ReadLine()))
                //{
                    if ("" == l.Trim()) continue;
                    //
                    if (n >= maxCount) break;
                    /*
                    using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
                    {
                        System.IO.StreamReader str = new System.IO.StreamReader(memoryStream);
                        System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(toType);
                        return xSerializer.Deserialize(str);
                    }
                    */

                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(l)))
                    {
                        try {
                            VideoFile vf = serializer.ReadObject(ms) as VideoFile;
                            if (null != vf)
                            {
                                vfl.Add(vf);
                                n++;
                            }
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                //}
            }
            return n;
        }
    }
    //class
}//namespace
